﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PSTaskDialog;
namespace smx
{
    public partial class compAssignForm : Form
    {
        public compAssignForm()
        {
            InitializeComponent();
        }

        //---------------------------------------------------------------------------------------------------------------------
        //when the user clicks the finished button
        //---------------------------------------------------------------------------------------------------------------------
        private void closeBtn_Click(object sender, EventArgs e)
        {
            if (
               cTaskDialog.MessageBox(this,
                   "Confirmation",
                   "Are you finished?",
                   "If you are finished click yes or else to carry on click no. You can always return here later if you have more components to assign.",
                   eTaskDialogButtons.YesNo,
                   eSysIcons.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        //---------------------------------------------------------------------------------------------------------------------
        //loads the components and the products into the combo boxes
        //---------------------------------------------------------------------------------------------------------------------
        private void compAssignForm_Load(object sender, EventArgs e)
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();
            
            //components fetch
            try
            {
                var q = from c in data.components
                        select new
                        {
                            c.id,
                            c.name
                        };
                compsCombo.DisplayMember = "name";
                compsCombo.ValueMember = "id";
                compsCombo.DataSource = q;
            }
            catch (Exception err )
            {
                cTaskDialog.MessageBox(this, "Error in Code",
                      "There was an error!",
                      "There was an error in attempting to get the record, there could be many reasons. IF the problem persists please let us know, for more information view the details " + err.Message,
                      err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
            }

            //products fetch
            try
            {
                var q = from p in data.products
                        select new
                        {
                            p.id,
                            p.name
                        };
                prodsCombo.DisplayMember = "name";
                prodsCombo.ValueMember = "id";
                prodsCombo.DataSource = q;
            }
            catch (Exception err)
            {
                cTaskDialog.MessageBox(this, "Error in Code",
                      "There was an error!",
                      "There was an error in attempting to get the record, there could be many reasons. IF the problem persists please let us know, for more information view the details " + err.Message,
                      err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
            }
        }

        //---------------------------------------------------------------------------------------------------------------------
        //the logic for the assign button
        //---------------------------------------------------------------------------------------------------------------------
        private void assignBtn_Click(object sender, EventArgs e)
        {
            bool exists = CheckIfExists();

            //check the user has assigned a qty or else throw an error message
            if (qtyTxt.Text == "")
            {
                cTaskDialog.MessageBox(this,
             "User Error",
             "Qty Needed Cannot Be Null",
             "Please Enter a Quantity in the Quantity needed Text Box. This application will give errors if the qty needed is zero.",
             eTaskDialogButtons.OK,
             eSysIcons.Error);
            }

            //if this already exists
            else if (exists == true)
            {
                cTaskDialog.MessageBox(this,
               "User Error",
               "This assignment is already in place",
               "Please assign a component that is not already assigned to this product. To change qty needed please go to the products details form and click on the components tab.",
               eTaskDialogButtons.OK,
               eSysIcons.Error);
            }
            //make the alloc
            else
            {
                stockmanagerexpressEntities data = new stockmanagerexpressEntities();
                try
                {
                    keyGen gen = new keyGen();
                    long aid = gen.generateId();

                    long qty = Convert.ToInt64(qtyTxt.Text);
                    long sid = (long)compsCombo.SelectedValue;
                    long pid = (long)prodsCombo.SelectedValue;
                    alloc a = new alloc();

                    a.id = aid;
                    a.qtyneeded = qty;
                    a.product_id = pid;
                    a.component_id = sid;
                    data.AddToallocs(a);
                    data.SaveChanges();

                    //confirm on the text box
                    confirmationTxt.Text += "Added " + compsCombo.Text + " to " + prodsCombo.Text + " Qty " + qtyTxt.Text + Environment.NewLine;

                }
                catch (Exception err)
                {
                    cTaskDialog.MessageBox(this, "Error in Code",
                      "There was an error!",
                      "There was an error in attempting to assign this component to this product, there could be many reasons. IF the problem persists please let us know, for more information view the details " + err.Message,
                      err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
                }
            }
        }


        private bool CheckIfExists()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();
            bool exists;
            long sid = (long)compsCombo.SelectedValue;
            long pid = (long)prodsCombo.SelectedValue;
            //this will throw an error if there is none so we catch the error and that ironically allows us in the main
            //method to add a new allocation , not pretty but it works
            try
            {
                 var check = (from a in data.allocs
                        where a.component_id == sid &&
                        a.product_id == pid
                        select a).First();
                exists = true;
            }
            catch (Exception err)
            {
                exists = false;
            }
         
            return exists;

        }
    }
}
